clear
clear matrix
clear mata
set more off, perm
set matsize 11000
set maxvar 30000
cd /Users/zachbrown/Projects/PriceTransparency/Data/
global output /Users/zachbrown/Projects/PriceTransparency/Output

// Open visit dataset
use clean/radiology_visit_3.dta, clear


// Make treatment indicator
gen post = (ym>=ym(2007,3))
gen web_post = (visit_on_web==1 & post==1)


// Connecticut General owned Cigna
replace payernm_short = 3 if payernm_short==4

// Matthew THornton owned by Anthem
replace payernm_short = 2 if payernm_short==6

// Keep only member info
keep year member_key tier std_product_type payernm_short member_zip male age zip* has* charlindex proc_emerg7
duplicates drop
sort year

gen n_claims = 1
collapse (sum) n_claims proc_emerg ///
	(firstnm) tier std_product_type payernm_short member_zip ///
	male age zip* has* charlindex ///
	, by(member_key)


// Prep vars
replace age = . if age<0
label var age "Age"
label var male "Male"
label var charlindex "Charlson Comorbidity Index"

label var zip_mean_income "Zip income (1000s)"
label var zip_median_income "Zip income (1000s)"
replace zip_mean_income = zip_mean_income/1000
replace zip_median_income = zip_median_income/1000
label var zip_pct_hs "Zip more than HS Degree"
label var zip_pct_ba "Zip more than BA Degree"
label var has_deduct "Plan has Deductible"
label var has_copay "Plan has Copay"
label var has_coin "Plan has Coinsurance"

// Fix product type label
label list std_product_type
label define std_product_type2 ///
	1 "PPO" ///
	2 "POS" ///
	3 "HMO" ///
	4 "Special Needs 1" ///
	5 "Special Needs 2" ///
	6 "Special Needs 3" ///
	7 "Child HIP" ///
	8 "EPO" ///
	9 "Self-Funded" ///
	10 "Stop Loss" ///
	11 "Indemnity"
label values std_product_type std_product_type2

// Clean insurer
label values payernm_short payernm_short
rename payernm_short payer_name_abbr
replace payer_name_abbr = 7 if inlist(payer_name_abbr,1,8) // Relabel Aetna and UnitedHealth as other

xi i.std_product_type i.payer_name_abbr i.tier, noomit

desc
levelsof std_product_type, local(levels)
foreach i of local levels {
 	local label_`i' : label (std_product_type) `i'
	label var _Istd_produ_`i' "`label_`i''"
}

levelsof payer_name_abbr, local(levels)
foreach i of local levels {
 	local label_`i' : label (payer_name_abbr) `i'
	label var _Ipayer_nam_`i' "`label_`i''"
}

levelsof tier, local(levels)
foreach i of local levels {
 	local label_`i' : label (tier) `i'
	label var _Itier_`i' "`label_`i''"
}


// Panel A
local sum_vars "male age charlindex zip_median_income zip_pct_ba" 
local n_vars: word count `sum_vars'
disp `n_vars'

estpost summarize `sum_vars'
eststo summA

esttab summA using $output/indiv_summary_a.tex, frag ///
	cells("mean(fmt(2 1 1 1 1) label( ) ) sd(fmt(2 1 1 1 1) label( ) ) min(fmt(0 1 0 1 1) label( ) ) max(fmt(0 1 0 1 1) label( ) )" ) ///
	stats() /// 
	delimiter(&) end(\\) ///
 	noobs collabels(none) booktabs gaps nonumber label  nonote replace nomtitles nodepvars 
	

// Panel B
gen Other_produ = _Istd_produ_11 | _Istd_produ_0==1 
label var Other_produ "Other"
local sum_vars "_Istd_produ_1 _Istd_produ_2 _Istd_produ_3 _Istd_produ_8 Other_produ" 
local n_vars: word count `sum_vars'
disp `n_vars'

estpost summarize `sum_vars'
eststo summB


esttab summB using $output/indiv_summary_b.tex, frag ///
	cells("mean(fmt(2) label( ) ) sd(fmt(2) label( ) ) min(fmt(0) label( ) ) max(fmt(0) label( ) )" ) ///
	stats() 
	delimiter(&) end(\\) nolines ///
 	noobs collabels(none) booktabs gaps nonumber label  nonote replace nomtitles nodepvars 
	


// Panel C
gen Other_payer = _Ipayer_nam_7==1 | _Ipayer_nam_0==1 
label var Other_payer "Other"
local sum_vars "_Ipayer_nam_2 _Ipayer_nam_3 _Ipayer_nam_5 Other_payer" 
local n_vars: word count `sum_vars'
disp `n_vars'

estpost summarize `sum_vars'
eststo summC


esttab summC using $output/indiv_summary_c.tex, frag ///
	cells("mean(fmt(2) label( ) ) sd(fmt(2) label( ) ) min(fmt(0) label( ) ) max(fmt(0) label( ) )" ) ///
	stats() 
	delimiter(&) end(\\) nolines ///
 	noobs collabels(none) booktabs gaps nonumber label  nonote replace nomtitles nodepvars 
	


// Panel D
local sum_vars "has_deduct has_copay has_coin" 
local n_vars: word count `sum_vars'
disp `n_vars'

estpost summarize `sum_vars'
eststo summD


esttab summD using $output/indiv_summary_d.tex, frag ///
	cells("mean(fmt(2) label( ) ) sd(fmt(2) label( ) ) min(fmt(0) label( ) ) max(fmt(0) label( ) )" ) ///
	stats() /// 
	delimiter(&) end(\\) nolines ///
 	noobs collabels(none) booktabs gaps nonumber label  nonote replace nomtitles nodepvars 
	

// Number of obs

esttab summD using $output/indiv_summary_e.tex, frag ///
	drop(*) cells("mean(fmt(2) label( ) ) sd(fmt(2) label( ) ) min(fmt(0) label( ) ) max(fmt(0) label( ) )" ) ///
	stats(N, fmt(%12.0fc) labels("Individuals")  layout("\multicolumn{1}{c}{@}")) ///
	varwidth(20) modelwidth(12) delimiter(&) end(\\) collabels(none) nolines ///
	nonumber label  nonote replace nomtitles nodepvars noisily 

